﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using Newtonsoft.Json;
using System.Data;

namespace TRPOP.CMS.Web.Admin
{
    public partial class editUserDefinedLink_list : BaseAdmin
    {

        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.Content_AllControl;
        }
        #endregion

        #region 页面变量
        protected FormInputValueManage m_FormInputValueManage = new FormInputValueManage();
        protected int m_intTypeid = 0;
        protected string m_strSetid = string.Empty;
        protected string m_strAction = string.Empty;
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {

            LoadBase();

            if (string.IsNullOrEmpty(m_strSetid) || m_intTypeid <= 0)
                return;

            BindData();

        }

        #endregion

        #region 页面方法

        public void LoadBase()
        {
            m_intTypeid = RequestHelper.RequestInt("typeid");
            m_strSetid = RequestHelper.RequestString("setid");
            m_strAction = RequestHelper.RequestString("ac");
            if (m_strAction == "ajax_get_linksdata")
            {

                #region 获取相关文章信息

                JavaScriptObject result = new JavaScriptObject();
                JavaScriptObject items = new JavaScriptObject();

                string strIds = RequestHelper.RequestString("id").Replace("'", "");

                if (strIds.IsEmpty())
                    return;

                if (strIds.StartsWith(","))
                    strIds = "0" + strIds;

                if (strIds.EndsWith(","))
                    strIds = strIds + "0";

                strIds = strIds.SQLFilter();

                if (strIds != "" || strIds.IndexOf(",") > 0)
                {
                    try
                    {
                        foreach (FormInputValueInfo oinfo in m_FormInputValueManage.GetListByCondition(0, " AND [FormInputValueId] in (" + strIds + ") ", ""))
                        {
                            JavaScriptObject item = new JavaScriptObject();
                            item.Add("id", oinfo.FormInputValueId);
                            item.Add("title", oinfo.Name);
                            items.Add(oinfo.FormInputValueId.ToString(), item);
                        }
                        result.Add("result", "1");
                        result.Add("msg", "ok");
                        result.Add("items", items);
                    }
                    catch (Exception error)
                    {
                        result.Add("result", "-1");
                        result.Add("msg", error.Message);
                    }
                }

                Response.Write(JavaScriptConvert.SerializeObject(result));
                base.End();

                #endregion

            }
        }

        public void BindData()
        {

            if (m_intTypeid > 0 && string.IsNullOrEmpty(m_strSetid) == false)
            {
                List<FormInputValueInfoWithLevel> m_listInfo = m_FormInputValueManage.GetFormInputValueListTree(m_intTypeid, 0, -1, "");
                foreach (FormInputValueInfoWithLevel oinfo in m_listInfo)
                {
                    oinfo.Name = PageAssist.AppendStr(oinfo.Level, "——") + oinfo.Name;
                }
                rptList.DataSource = m_listInfo;
                rptList.DataBind();
            }

        }
        #endregion


    }
}
